/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myprogramui.comp;

import Globals.AllGlobal;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import model.GongZuoMian_Model;
import model.ZuanChang_Model;
import model.ZuanKong_Model;
import myprogramui.comp.bean.TableInfo;

/**
 *
 * @author Administrator
 */
public class AnalysisPage extends Group{
    private Stage primaryStage;
    private Rectangle center;
    private Line line;
    //坏孔barchart
    XYChart.Series series1 = new XYChart.Series();
    XYChart.Series series2 = new XYChart.Series();
    XYChart.Series series3 = new XYChart.Series();
    final CategoryAxis xAxis1 = new CategoryAxis();
    final NumberAxis yAxis1 = new NumberAxis();
    final BarChart<String,Number> bchk = 
        new BarChart<String,Number>(xAxis1,yAxis1);
    //终压barchart
    XYChart.Series series4 = new XYChart.Series();
    XYChart.Series series5 = new XYChart.Series();
    XYChart.Series series6 = new XYChart.Series();
    final CategoryAxis xAxis2 = new CategoryAxis();
    final NumberAxis yAxis2 = new NumberAxis();
    final BarChart<String,Number> bczy = 
        new BarChart<String,Number>(xAxis2,yAxis2);
    //水压barchart
    XYChart.Series series7 = new XYChart.Series();
    XYChart.Series series8 = new XYChart.Series();
    XYChart.Series series9 = new XYChart.Series();
    final CategoryAxis xAxis3 = new CategoryAxis();
    final NumberAxis yAxis3 = new NumberAxis();
    final BarChart<String,Number> bcsy = 
        new BarChart<String,Number>(xAxis3,yAxis3);
    
    final static String youxiao = "有效孔";
    final static String weixian = "危险孔";
    final static String fuhe = "终压符合";
    final static String bufuhe = "终压不符";
    final static String xiao = "0-29";
    final static String zhong = "30-59";
    final static String da = "60-89";
    final static String teda = "90-_";
    final static String oth = "其他";
    //距离
    private static XYChart.Data<String,Integer> jiaoshun_ok_Data;
    private static XYChart.Data<String,Integer> jiaoshun_no_Data;
    private static XYChart.Data<String,Integer> guishun_ok_Data;
    private static XYChart.Data<String,Integer> guishun_no_Data;
    private static XYChart.Data<String,Integer> gzm_ok_Data;
    private static XYChart.Data<String,Integer> gzm_no_Data;

    //终压
    private static XYChart.Data<String,Integer> jiaoshun_fuhe_Data;
    private static XYChart.Data<String,Integer> jiaoshun_bufuhe_Data;
    private static XYChart.Data<String,Integer> guishun_fuhe_Data;
    private static XYChart.Data<String,Integer> guishun_bufuhe_Data;
    private static XYChart.Data<String,Integer> gzm_fuhe_Data;
    private static XYChart.Data<String,Integer> gzm_bufuhe_Data;

    //水压
    private static XYChart.Data<String,Integer> jiaoshun_xiao_Data;
    private static XYChart.Data<String,Integer> jiaoshun_zhong_Data;
    private static XYChart.Data<String,Integer> jiaoshun_da_Data;
    private static XYChart.Data<String,Integer> jiaoshun_teda_Data;
    private static XYChart.Data<String,Integer> jiaoshun_oth_Data;
    private static XYChart.Data<String,Integer> guishun_xiao_Data;
    private static XYChart.Data<String,Integer> guishun_zhong_Data;
    private static XYChart.Data<String,Integer> guishun_da_Data;
    private static XYChart.Data<String,Integer> guishun_teda_Data;
    private static XYChart.Data<String,Integer> guishun_oth_Data;
    private static XYChart.Data<String,Integer> gzm_xiao_Data;
    private static XYChart.Data<String,Integer> gzm_zhong_Data;
    private static XYChart.Data<String,Integer> gzm_da_Data;
    private static XYChart.Data<String,Integer> gzm_oth_Data;
    private static XYChart.Data<String,Integer> gzm_teda_Data;
    //PieChart
    private static PieChart.Data ahcs=new PieChart.Data("八灰层上", 1);
    private static PieChart.Data ahcz=new PieChart.Data("八灰层中", 1);
    private static PieChart.Data ahcx=new PieChart.Data("八灰层下", 1);
    //table表中内容
    final ObservableList<TableInfo> alltableInfos = FXCollections.observableArrayList(); 
    //设置drillpage
    DrillPage drillPage;
    public AnalysisPage(Line line,Rectangle center,Stage primaryStage,final DrillPage drillPage){
        this.drillPage=drillPage;
        ahcs=new PieChart.Data("八灰层上", 1);
        this.primaryStage=primaryStage;
        this.center=center;
        this.line=line;
        //初始化柱体
        jiaoshun_ok_Data=new XYChart.Data<String,Integer>(youxiao, 1);
        jiaoshun_no_Data=new XYChart.Data<String,Integer>(weixian, 1);
        guishun_ok_Data=new XYChart.Data<String,Integer>(youxiao, 1);
        guishun_no_Data=new XYChart.Data<String,Integer>(weixian, 1);
        gzm_ok_Data=new XYChart.Data<String,Integer>(youxiao, 1);
        gzm_no_Data=new XYChart.Data<String,Integer>(weixian, 1);
        
        jiaoshun_fuhe_Data=new XYChart.Data<String,Integer>(fuhe, 1);
        jiaoshun_bufuhe_Data=new XYChart.Data<String,Integer>(bufuhe, 1);
        guishun_fuhe_Data=new XYChart.Data<String,Integer>(fuhe, 1);
        guishun_bufuhe_Data=new XYChart.Data<String,Integer>(bufuhe, 1);
        gzm_fuhe_Data=new XYChart.Data<String,Integer>(fuhe, 1);
        gzm_bufuhe_Data=new XYChart.Data<String,Integer>(bufuhe, 1);
        
        jiaoshun_xiao_Data=new XYChart.Data<String,Integer>(xiao, 1);
        jiaoshun_zhong_Data=new XYChart.Data<String,Integer>(zhong, 1);
        jiaoshun_da_Data=new XYChart.Data<String,Integer>(da, 1);
        jiaoshun_teda_Data=new XYChart.Data<String, Integer>(teda,1);
        jiaoshun_oth_Data=new XYChart.Data<String, Integer>(oth,1);
        guishun_xiao_Data=new XYChart.Data<String,Integer>(xiao, 1);
        guishun_zhong_Data=new XYChart.Data<String,Integer>(zhong, 1);
        guishun_da_Data=new XYChart.Data<String,Integer>(da, 1);
        guishun_teda_Data=new XYChart.Data<String, Integer>(teda,1);
        guishun_oth_Data=new XYChart.Data<String, Integer>(oth,1);
        gzm_xiao_Data=new XYChart.Data<String,Integer>(xiao, 1);
        gzm_zhong_Data=new XYChart.Data<String,Integer>(zhong, 1);
        gzm_da_Data=new XYChart.Data<String,Integer>(da, 1);   
        gzm_teda_Data=new XYChart.Data<String, Integer>(teda,1);
        gzm_oth_Data=new XYChart.Data<String, Integer>(oth,1);
        //添加柱状图 坏孔
        
        bchk.setTitle("好坏孔统计");
        xAxis1.setLabel("孔类型");       
        yAxis1.setLabel("个数");  
        series1.setName("胶顺");       
        series1.getData().add(jiaoshun_ok_Data);
        series1.getData().add(jiaoshun_no_Data);    
        series2.setName("轨顺");
        series2.getData().add(guishun_ok_Data);
        series2.getData().add(guishun_no_Data);          
        series3.setName("工作面");
        series3.getData().add(gzm_ok_Data);
        series3.getData().add(gzm_no_Data);
        bchk.getData().addAll(series1, series2, series3);
        bchk.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(85));
        bchk.setTranslateY(140);
        bchk.setPrefWidth(250);
        bchk.setPrefHeight(250);
        this.getChildren().add(bchk);
        
        //添加柱状图 终压

        bczy.setTitle("终压统计");
        xAxis2.setLabel("孔类型");       
        yAxis2.setLabel("个数");  
        series4.setName("胶顺");       
        series4.getData().add(jiaoshun_fuhe_Data);
        series4.getData().add(jiaoshun_bufuhe_Data);    
        series5.setName("轨顺");
        series5.getData().add(guishun_fuhe_Data);
        series5.getData().add(guishun_bufuhe_Data);          
        series6.setName("工作面");
        series6.getData().add(gzm_fuhe_Data);
        series6.getData().add(gzm_bufuhe_Data);
        bczy.getData().addAll(series4, series5, series6);
        bczy.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(335));
        bczy.setTranslateY(140);
        bczy.setPrefWidth(250);
        bczy.setPrefHeight(250);
        this.getChildren().add(bczy);
        //添加柱状图 终压
        bcsy.setTitle("水压统计");
        xAxis3.setLabel("水压（MPa）");       
        yAxis3.setLabel("个数");  
        series7.setName("胶顺");       
        series7.getData().add(jiaoshun_xiao_Data);
        series7.getData().add(jiaoshun_zhong_Data); 
        series7.getData().add(jiaoshun_da_Data);  
        series7.getData().add(jiaoshun_teda_Data);
        series7.getData().add(jiaoshun_oth_Data);
        series8.setName("轨顺");
        series8.getData().add(guishun_xiao_Data);
        series8.getData().add(guishun_zhong_Data); 
        series8.getData().add(guishun_da_Data);  
        series8.getData().add(guishun_teda_Data);
        series8.getData().add(guishun_oth_Data);
        series9.setName("工作面");
        series9.getData().add(gzm_xiao_Data);
        series9.getData().add(gzm_zhong_Data); 
        series9.getData().add(gzm_da_Data); 
        series9.getData().add(gzm_teda_Data);
        series9.getData().add(gzm_oth_Data);
        bcsy.getData().addAll(series7, series8, series9);
        bcsy.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(585));
        bcsy.setTranslateY(140);
        bcsy.setPrefWidth(250);
        bcsy.setPrefHeight(250);
        this.getChildren().add(bcsy);
        //放置表格
        TableView<TableInfo> tv=new TableView<TableInfo>();
        final ObservableList<TableInfo> tableInfos = FXCollections.observableArrayList(); 
        tv.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(85));
        tv.translateYProperty().bind(bchk.translateYProperty().add(280));
        tv.setPrefWidth(1020);
        tv.prefHeightProperty().bind(primaryStage.heightProperty().divide(3.0));
        tv.setItems(tableInfos); 

        TableColumn<TableInfo, String> nameCol = new TableColumn<TableInfo, String>("钻场名称"); 
        nameCol.setEditable(true);
        nameCol.setCellValueFactory(new PropertyValueFactory("zuanchangNameP")); 
        nameCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> haoCol = new TableColumn<TableInfo, String>("有效孔"); 
        haoCol.setEditable(true);
        haoCol.setCellValueFactory(new PropertyValueFactory("haokongP")); 
        haoCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> huaiCol = new TableColumn<TableInfo, String>("危险孔"); 
        huaiCol.setEditable(true);
        huaiCol.setCellValueFactory(new PropertyValueFactory("huaikongP")); 
        huaiCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> fuheCol = new TableColumn<TableInfo, String>("终压符合"); 
        fuheCol.setEditable(true);
        fuheCol.setCellValueFactory(new PropertyValueFactory("fuheP")); 
        fuheCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> bufuCol = new TableColumn<TableInfo, String>("终压不符"); 
        bufuCol.setEditable(true);
        bufuCol.setCellValueFactory(new PropertyValueFactory("bufuP")); 
        bufuCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> xiaoCol = new TableColumn<TableInfo, String>("水压0-29"); 
        xiaoCol.setEditable(true);
        xiaoCol.setCellValueFactory(new PropertyValueFactory("smallP")); 
        xiaoCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> zhongCol = new TableColumn<TableInfo, String>("水压30-59"); 
        zhongCol.setEditable(true);
        zhongCol.setCellValueFactory(new PropertyValueFactory("zhongP")); 
        zhongCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> daCol = new TableColumn<TableInfo, String>("水压60-89"); 
        daCol.setEditable(true);
        daCol.setCellValueFactory(new PropertyValueFactory("daP")); 
        daCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> tedaCol = new TableColumn<TableInfo, String>("水压90-_"); 
        daCol.setEditable(true);
        daCol.setCellValueFactory(new PropertyValueFactory("tedaP")); 
        daCol.setPrefWidth(tv.getPrefWidth() / 10); 
        TableColumn<TableInfo, String> othdaCol = new TableColumn<TableInfo, String>("水压 其他"); 
        daCol.setEditable(true);
        daCol.setCellValueFactory(new PropertyValueFactory("othdaP")); 
        daCol.setPrefWidth(tv.getPrefWidth() / 10); 
        tv.getColumns().addAll(nameCol,huaiCol,haoCol,fuheCol,bufuCol,xiaoCol,zhongCol,daCol,tedaCol,othdaCol);
        this.getChildren().add(tv);
        //出水点统计chart
        ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                ahcs,
                ahcz,
                ahcx);
        final PieChart chart = new PieChart(pieChartData);
        chart.translateXProperty().bind(tv.translateXProperty().add(780));
        chart.translateYProperty().bind(bchk.translateYProperty());
        chart.setPrefSize(250, 250);
        chart.setTitle("出水点统计");
        this.getChildren().add(chart);
        //添加绘制按钮和绘制进度条
        Button drawBut = new Button("统计表格生成");
        
        drawBut.setTranslateX(300);
        drawBut.translateYProperty().bind(bchk.translateYProperty().subtract(30));
        this.getChildren().add(drawBut);
        
        final ProgressBar pb=new ProgressBar();
        pb.setProgress(0);
        pb.setPrefWidth(540);
        pb.setTranslateX(470);
        pb.translateYProperty().bind(bchk.translateYProperty().subtract(27));
        this.getChildren().add(pb);
        //添加绘制事件
        drawBut.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                try {
                    int progress=0;
                    //好坏系列
                    int jsgood=0;
                    int jsbad=0;
                    int gsgood=0;
                    int gsbad=0;
                    int gzmgood=0;
                    int gzmbad=0;
                    //终压系列
                    int jszyfh=0;
                    int jszybfh=0;
                    int gszyfh=0;
                    int gszybfh=0;
                    int gzmzyfh=0;
                    int gzmzybfh=0;
                    
                    //水压系列
                    int jsx=0;
                    int jsz=0;
                    int jsd=0;
                    int jstd=0;
                    int jsothdI=0;
                    int gsx=0;
                    int gsz=0;
                    int gsd=0;
                    int gstd=0;
                    int gsothd=0;
                    int gzmx=0;
                    int gzmz=0;
                    int gzmd=0;
                    int gzmtd=0;
                    int gzmothdI=0;
                    //奥灰层
                    float ahcsd=0;
                    int ahcsI=0;
                    int ahczI=0;
                    int ahcxI=0;
                    //计算好坏孔代码段

                    //这个让计算机去判断，我们的好坏孔放在一个单独的处理段内
                    ArrayList<GongZuoMian_Model> gzmlist=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
                    if(gzmlist.size()>0)
                        ahcsd=Float.parseFloat(gzmlist.get(0).getAhcjl());
                    ArrayList<ZuanKong_Model> zklist=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANKONG_SQL);
                    for(int i=0;i<zklist.size();i++){
                        
                        ZuanKong_Model zkm=zklist.get(i);
                        String zcm=zkm.getZuanchang();
                        String firstname=zcm.substring(0, 1);
                        if(firstname.equals("胶")){
                            //判断终压
                            if(Float.valueOf(zkm.getZhongya())<13){
                                jszybfh++;
                                jiaoshun_bufuhe_Data.setYValue(jszybfh);
                            }else{
                                jszyfh++;
                                jiaoshun_fuhe_Data.setYValue(jszyfh);
                            }
                            //判断注浆前水压
                            if(Float.valueOf(zkm.getZhujiangqian())<30){
                                jsx++;
                                jiaoshun_xiao_Data.setYValue(jsx);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<60){
                                jsz++;
                                jiaoshun_zhong_Data.setYValue(jsz);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<90){
                                jsd++;
                                jiaoshun_da_Data.setYValue(jsd);
                            }else if(Float.valueOf(zkm.getZhujiangqian())>=90){
                                jstd++;
                                jiaoshun_teda_Data.setYValue(jstd);
                            }
                            
                        }else if(firstname.equals("轨")){
                            //判断终压
                            if(Float.valueOf(zkm.getZhongya())<13){
                                gszybfh++;
                                guishun_bufuhe_Data.setYValue(gszybfh);
                            }else{
                                gszyfh++;
                                guishun_fuhe_Data.setYValue(gszyfh);
                            }
                            //判断注浆前水压
                            if(Float.valueOf(zkm.getZhujiangqian())<30){
                                gsx++;
                                guishun_xiao_Data.setYValue(gsx);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<60){
                                gsz++;
                                guishun_zhong_Data.setYValue(gsz);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<90){
                                gsd++;
                                guishun_da_Data.setYValue(gsd);
                            }else  if(Float.valueOf(zkm.getZhujiangqian())>=90){
                                gstd++;
                                guishun_teda_Data.setYValue(gstd);
                            }
                        }else{
                            //判断终压
                            if(Float.valueOf(zkm.getZhongya())<13){
                                gzmzybfh++;
                                gzm_bufuhe_Data.setYValue(gzmzybfh);
                            }else{
                                gzmzyfh++;
                                gzm_fuhe_Data.setYValue(gzmzyfh);
                            }
                            //判断注浆前
                            if(Float.valueOf(zkm.getZhujiangqian())<30){
                                gzmx++;
                                gzm_xiao_Data.setYValue(gzmx);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<60){
                                gzmz++;
                                gzm_zhong_Data.setYValue(gzmz);
                            }else if(Float.valueOf(zkm.getZhujiangqian())<90){
                                gzmd++;
                                gzm_da_Data.setYValue(gzmd);
                            }else if(Float.valueOf(zkm.getZhujiangqian())>=90){
                                gzmtd++;
                                gzm_teda_Data.setYValue(gzmtd);
                            }
                        }
                        //独立的判断与奥灰层的关系
                        float csdspty=Float.parseFloat(zkm.getChushuidianshuipingtouying());//水平投影
                        float qingjiao=Float.parseFloat(zkm.getQingjiao());//倾角
                        double czjl=csdspty*Math.tan(Math.abs(qingjiao)*Math.PI/180.0f);
                        if(35.65>czjl){
                            ahcsI++;
                            ahcs.setPieValue(ahcsI);
                        }else if(35.65>czjl-9.5){
                            ahcxI++;
                            ahcx.setPieValue(ahcxI);
                        }else{
                            ahczI++;
                            ahcz.setPieValue(ahczI);
                        }
                        
                    }
                    //单独设置那个危险与不危险
                    ArrayList<Integer> wxList=drillPage.getYXWXK_Chart();
                    jiaoshun_ok_Data.setYValue(wxList.get(0));
                    jiaoshun_no_Data.setYValue(wxList.get(1));
                    guishun_ok_Data.setYValue(wxList.get(2));
                    guishun_ok_Data.setYValue(wxList.get(3));
                    gzm_ok_Data.setYValue(wxList.get(4));
                    gzm_ok_Data.setYValue(wxList.get(5));
                    //按照钻场来处理
                    ArrayList<ZuanChang_Model> zclist=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANCHANG_SQL);
                    //取得我们的DRillPage里的钻场数据
                    ArrayList<Integer> tList=drillPage.getYXWXK_Table();
                    for(int j=0;j<zclist.size();j++){
                        if(zklist.size()>0){
                            ZuanChang_Model zcm=zclist.get(j);
                            String name=zcm.getMingcheng();
                            //处理进度
                            progress++;
                            pb.setProgress(progress*1.0/zclist.size());
                            int i1=tList.get(j);//hao
                            int i2=tList.get(j+zclist.size());//huai
                            int i3=0;//fuhe
                            int i4=0;//bufu
                            int i5=0;//xiao
                            int i6=0;//zhong
                            int i7=0;//da
                            int i8=0;//teda
                            int i9=0;//othda
                            for(int k=0;k<zklist.size();k++){
                                ZuanKong_Model myzkm=zklist.get(k);
                                if(myzkm.getZuanchang().equals(name)){
                                    if(Float.parseFloat(myzkm.getZhongya())<13){
                                        i4++;
                                    }else{
                                        i3++;
                                    }
                                    //xiao zhogn da
                                    if(Float.parseFloat(myzkm.getZhujiangqian())<30){
                                        i5++;
                                    }else if(Float.parseFloat(myzkm.getZhujiangqian())<60){
                                        i6++;
                                    }else if(Float.parseFloat(myzkm.getZhujiangqian())<90){
                                        i7++;
                                    }else if(Float.parseFloat(myzkm.getZhujiangqian())>=90){
                                        i8++;
                                    }else
                                        i9++;
                                }
                            }
                            TableInfo tableInfo=new TableInfo(name, i1, i2, i3, i4, i5, i6, i7,i8,i9);
                            tableInfos.add(tableInfo);
                        }
                    }
                } catch (Exception ex) {
                    Logger.getLogger(AnalysisPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
    }
}
